/**
 * 
 */
package com.util;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Properties;

public class ConstantUtil {

	private static ConstantUtil instance = null;
	public synchronized static ConstantUtil getInstance()
	{
		if (instance == null)
			instance = new ConstantUtil();
		return instance;
	}
	
	protected ConstantUtil()
	{
		
	}

	public static final Map<String, String> getConstant(String type)
	{
		// return constant.get(type);
		return constants.get(type);
	}
	
	public static final String getConstantString(String type, String value)
	{
		// System.out.println(type + ":" + value);
//		if (constant.containsKey(type) 
//				&& constant.get(type).containsKey(value))
//			return constant.get(type).get(value);
		if (constants.containsKey(type) 
				&& constants.get(type).containsKey(value))
			return constants.get(type).get(value);
		return "";
	}
	
	public static enum CONSTANT_TYPE
	{
		CARRIER,
		BUSINESS,
		CODE_STATUS,
		SEED_TYPE,
		SETTLEMENT_METHOD,
		SETTLEMENT_INTERVAL,
		BLACK_TYPE,
		CURRENCY_UNIT,
		TIME_UNIT
	}

	private static final Map<String, Map<String, String>> constants;
	private static final Map<String, Map<String, String>> constant;
	static 
	{
		constant = new HashMap<String, Map<String,String>>();
		
		Properties props = new Properties();
		try
		{
			props.load(new FileInputStream(ConstantUtil.class.getResource("/").getFile() +"constant.properties"));
			
			String constants = props.getProperty("constant", "");
			if (constants.length() >0)
			{
				for (String ct : constants.split(","))
				{
					if (ct.length() == 0)
						continue;
					
					constant.put(ct, new LinkedHashMap<String, String>());
					String ctkeys = props.getProperty("constant." + ct.replace(" ", "_"));
					if (ctkeys.length() > 0)
					{
						for (String ctk : ctkeys.split(","))
						{
							if (ctk.length() == 0)
								continue;
							
							String ctv = new String(props.getProperty("constant." + ct.replace(" ", "_") + "." + ctk.replace(" ", "_")).getBytes("ISO-8859-1"), "UTF-8");
							constant.get(ct).put(ctk, ctv);
						}
					}
				}
			}
			
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		
		constants = new HashMap<String, Map<String,String>>();
		try {
			String section = ".";
			constants.put(section, new LinkedHashMap<String, String>());
			BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(
					ConfigUtil.class.getResource("/constant.cfg").getFile()), "UTF-8"));
			String line;
			while ((line = reader.readLine()) != null) {
				line = line.trim();
				
				if (line.length() == 0 || line.startsWith("#"))
					continue;

				if (line.matches("\\[.*\\]")) {
					String sec = line.replaceFirst("\\[(.*)\\]", "$1").trim();
					if (!section.equals(sec)) {
						section = sec;
						constants.put(section, new LinkedHashMap<String, String>());
					}
				} else if (line.matches(".*=.*")) {
					int i = line.indexOf('=');
					String name = line.substring(0, i).trim();
					String value = line.substring(i + 1).trim();
					constants.get(section).put(name, value);
				}
			}
			reader.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	};

}
